﻿Public Class FormRole

    Public Sub loadData()
        CmdPro.Parameters("@S") = "%" & txtSearch.Text & "%"
        dgv.DataSource = CmdPro.ExecuteDataTable("QueryRole")

    End Sub
    Private Sub FormRole1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim frm As New FormRoleAED()
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            loadData()
        End If
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row As DataGridViewRow = dgv.SelectedRows(0)
        Dim roleId = row.Cells("RoleId").Value
        Dim roleName = row.Cells("RoleName").Value
        Dim salary = row.Cells("Salary").Value
        Dim department = row.Cells("GROUP_NAME").Value

        Dim frm As New FormRoleAED()
        frm.txtRoleId.Enabled = False
        frm.txtRoleId.Text = roleId
        frm.txtRoleName.Text = roleName
        frm.txtSalary.Text = salary
        frm.cboDepart.Text = department
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            loadData()
        End If

    End Sub


    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If


        Dim row As DataGridViewRow = Me.dgv.SelectedRows(0)
        Dim id = row.Cells("RoleId").Value

        CmdPro.Parameters("@Id") = id
        Dim count = CmdPro.ExecuteScalar("CountUserRole")

        If count > 0 Then
            MsgBox("Role is already in used")
            Exit Sub
        End If

        If MessageBox.Show("តើលោកអ្នកចង់លុបប្រភេទនៃក្រុមមួយនេះមែនទេ?", "លុបប្រភេទនៃក្រុម", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        CmdPro.Parameters("@Id") = id
        CmdPro.ExecuteNonQueryNoParameter("DeleteUserRole")

        loadData()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dbinterface.OpenConnection()
        loadData()
    End Sub
End Class